#include "StdAfx.h"
#include "Case.h"
#include "Step.h"
#include "Suit.h"
#include "../macro.h"

Case::Case(const char* name, Suit* parent)
: m_name(name)
, parent_(parent)
{
}

Case::~Case(void)
{
}

bool Case::addStep(Step* step)
{
	if (step==NULL)
	{
		return false;
	}

	m_steps.push_back(step);
	return true;
}

bool Case::delStep(int index)
{
	if (index>=m_steps.size())
	{
		return false;
	}

	int id_of_list = 0;
	for (std::vector<Step*>::iterator iter=m_steps.begin();iter!=m_steps.end();++iter,++id_of_list)
	{
		if (index == id_of_list)
		{
			m_steps.erase(iter);
			break;
		}
	}
	return true;
}

bool Case::run(void* param)
{
	DBG_PRINTF("%s\r\n",__FUNCTION__);
	MSG_PRINTF("  [Test_Case]%s \r\n", m_name.c_str());

	for (float index = getStart(); index < getEnd(); index += getStep())
	{
		for (std::vector<Step*>::iterator iter = m_steps.begin(); iter != m_steps.end(); ++iter)
		{
			if ((*iter) != NULL)
			{
				if (!(*iter)->run(param))
				{
					return false;
				}
			}
		}
	}

	return true;
}

std::string Case::getName()
{
	return m_name;
}

std::list<loopable *> Case::getLoopable()
{
	std::list<loopable *>  loop;
	if (parent_)
	{
		loop = parent_->getLoopable();
	}

	loop.push_back(this);

	return loop;
}